﻿using UnityEngine;
using System.Collections;

public class BM_DialogueToy : BM_BaseToy
{
	public enum Action
	{
		ACTION_NONE = -1
	}

	public StringLabel dialogueStart;
	public StringLabel dialogueEnd;

	#region Members

	// parametri da configurare tramite i prefab
	public 	MessageID	m_dialogCompletionMessage = MessageID.MSGID_NONE;
	public 	MessageID	m_stopInteractionMessage = MessageID.MSGID_NONE;

	private TextMesh	m_textMesh;
	private GameObject	m_textBack;
	private StringLabel m_currentLabel;

	protected AudioClip[]	m_rTalkAudioClip = null;

	private float			m_fPlayStepTimeStamp = 0.0f;
	private float			m_fPlayStepEvery = 1.0f;

	#endregion


	public override void Awake ()
	{
		base.Awake ();

		m_rFSM.AddState(new BM_FSM.StateInfo((int)State.STATE_IDLE, GetType()+".STATE_IDLE", InitializeIdleState, UpdateIdleState));
		m_rFSM.AddState(new BM_FSM.StateInfo((int)State.STATE_CUT, GetType()+".STATE_CUT", InitializeCutState, UpdateCutState));
		m_rFSM.AddState(new BM_FSM.StateInfo((int)State.STATE_ACTION, GetType()+".STATE_ACTIVE", InitializeActionState, UpdateActionState));
		m_rFSM.NextState = (int)State.STATE_IDLE;

		//TODO: ottiene e inizializza il trigger di attivazione

		// IDLE: l'ggetto non è attivo, se il giocatore ci interagisce va direttamente nello stato ACTION
		// ACTIVE: l'oggetto è attivo, se il giocatore ci interagisce va allo stato CUT (si trasforma)
		// CUT: il gioco si ferma (il controller del giocatore è disattivato), l'oggetto in questione si trasforma, al termine dell'animazione passa allo stato ACTION
		// ACTION: l'ggetto viene raccolto, posizione e rotazione vengono interpolate per finire in mano al giocatore (si deve sapere su quale mano finirà l'oggetto)

		m_textMesh = GameObject.FindWithTag("Text").GetComponent("TextMesh") as TextMesh;
		m_textBack = GameObject.FindWithTag("TextBack");

		m_bSelectable = m_bQuestActive;

		m_rTalkAudioClip = new AudioClip[2];
		m_rTalkAudioClip[0] = Resources.Load("sfx_talk_01") as AudioClip;
		m_rTalkAudioClip[1] = Resources.Load("sfx_talk_02") as AudioClip;
	}

	public override void Update ()
	{
		base.Update ();

		BM_BaseMessage rBaseMsg = m_rMessageHandler.ReceivedMessageWithID(m_stopInteractionMessage);
		if(rBaseMsg != null)
		{
			if(rBaseMsg.m_eMessageID > MessageID.MSGID_QUEST_START && rBaseMsg.m_eMessageID < MessageID.MSGID_QUEST_END)
				m_bSelectable = false;
		}
	}

	private void InitializeIdleState()
	{
		m_fPlayStepTimeStamp = Time.time;
	}

	private void UpdateIdleState()
	{
		if(m_rMessageHandler.ReceivedMessageWithID(MessageID.MSGID_TOY_PERFORM_ACTION) != null && m_bSelectable)
		{
			if(m_bQuestActive) {
				m_rFSM.NextState = (int)State.STATE_CUT;
				m_rMessageHandler.SendMessageToAllButMe(new BM_BaseMessage(MessageID.MSGID_STOP_INTERACTION, MessageHandler));
			} else
				m_rFSM.NextState = (int)State.STATE_ACTION;
		}

		
		if(audio && m_bQuestActive) {
			if(Time.time >= m_fPlayStepTimeStamp)
			{
				audio.Play();
				m_fPlayStepTimeStamp = Time.time + m_fPlayStepEvery + Random.Range(1,3);
			}
		}
	}

	private void InitializeActionState()
	{
		// inizializzo dialogo
		m_currentLabel = dialogueStart;
		m_textMesh.text = BM_Common.GetLabelName(m_currentLabel);
		MidDialog(m_currentLabel);
		m_textMesh.renderer.enabled = true;
		m_textBack.renderer.enabled = true;

		AudioSource.PlayClipAtPoint(m_rTalkAudioClip[Random.Range(0, m_rTalkAudioClip.Length)], Vector3.zero, 1.0f);

		m_rMessageHandler.SendMessageToAllButMe(new BM_BaseMessage(MessageID.MSGID_STOP_INTERACTION, MessageHandler));
	}
	
	private void UpdateActionState()
	{
		if(Input.GetMouseButtonDown(0)) {
			if(m_currentLabel < dialogueEnd) {
				m_currentLabel++;
				m_textMesh.text = BM_Common.GetLabelName(m_currentLabel);

				MidDialog(m_currentLabel);

				AudioSource.PlayClipAtPoint(m_rTalkAudioClip[Random.Range(0, m_rTalkAudioClip.Length)], Vector3.zero, 1.0f);
			} else {
				// dialogue finished

				DialogueComplete();

				m_textMesh.renderer.enabled = false;
				m_textBack.renderer.enabled = false;
				m_rMessageHandler.SendMessageToAllButMe(new BM_BaseMessage(MessageID.MSGID_START_INTERACTION, MessageHandler));

				if(m_dialogCompletionMessage != MessageID.MSGID_NONE) {
					m_rMessageHandler.SendMessageToAllButMe(new BM_QuestActivationMessage(m_dialogCompletionMessage, MessageHandler, true));
				}

				m_bQuestActive = false;
				m_rFSM.NextState = (int)State.STATE_IDLE;

			}
		}
	}

	public virtual void MidDialog(StringLabel m_currentLabel) {
		// to override...
	}

	public virtual void DialogueComplete() {
		// to override...
	}
}
